home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / lib / time / localtime.c < prev    next >
C/C++ Source or Header  |  1997-09-09  |  2KB  |  65 lines

  1.  
  2. /*
  3.  *  time/localtime.c
  4.  *
  5.  *    (c)Copyright 1992-1997 Obvious Implementations Corp.  Redistribution and
  6.  *    use is allowed under the terms of the DICE-LICENSE FILE,
  7.  *    DICE-LICENSE.TXT.
  8.  */
  9.  
  10. #include <time.h>
  11. #include <lib/misc.h>
  12.  
  13. static char dim[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  14.  
  15. struct tm *
  16. localtime(tp)
  17. const time_t *tp;
  18. {
  19.     static struct tm Tm;
  20.     return(localtime_tm(tp, &Tm));
  21. }
  22.  
  23. struct tm *
  24. localtime_tm(tp, tm)
  25. const time_t *tp;
  26. struct tm *tm;
  27. {
  28.     time_t t = (tp) ? *tp : time(NULL);     /*    seconds since 1-jan-78    */
  29.     long days, years;
  30.     short leap, month;
  31.  
  32.     t = t - _TimeCompensation;
  33.  
  34.     days = t / 86400 + 731;            /*    days since 1976 (lyp yr)*/
  35.  
  36.     tm->tm_wday = (days + 4) % 7;
  37.     tm->tm_sec = t % 60;
  38.     tm->tm_min = t / 60 % 60;
  39.     tm->tm_hour= t / (60*60) % 24;
  40.  
  41.     years = days / (366+365*3);         /*    #quad yrs    */
  42.     days -= years * (366+365*3);        /*    days remaining    */
  43.                         /*    0 = jan 1    */
  44.     leap = (days <= 365);            /*    0-365, is a leap yr */
  45.     years = 1976 + 4 * years;            /*    base yr     */
  46.     if (leap == 0) {                /*    days >= 366    */
  47.     days -= 366;                /*    add a year    */
  48.     ++years;
  49.     years += days / 365;            /*    non-lyrs left    */
  50.     days  %= 365;                /*    0-364        */
  51.     }
  52.     tm->tm_yday = days;
  53.     tm->tm_year = years - 1900;
  54.     tm->tm_isdst= -1; /* ???? XXX     */
  55.  
  56.     for (month = 0; (month==1) ? (days >= 28 + leap) : (days >= dim[month]); ++month)
  57.     days -= (month==1) ? (28 + leap) : dim[month];
  58.  
  59.     tm->tm_mon    = month;
  60.     tm->tm_mday = days + 1;
  61.  
  62.     return(tm);
  63. }
  64.  
  65.